package org.example.bean.entity.authorization;

import com.bianmaba.spring.data.jpa.domain.annotations.QueryCacheable;
import com.bianmaba.spring.data.jpa.domain.entity.UuidIdentityEntity;
import lombok.Data;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@DynamicUpdate()
@DynamicInsert()
@Entity
@Table(name = "System_Client", uniqueConstraints = {@UniqueConstraint(columnNames = {"clientId"})})
//此注解重新定义为是否开启查询缓存
@QueryCacheable
//此注解表示此类支持二级缓存，使用默认缓存（由缓存管理器根据ehcache.xml中的defaultCache自动创建）
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "Client")
@Data
public class Client extends UuidIdentityEntity {
    private static final long serialVersionUID = -4944044646898487415L;

    /**
     * 客户端ID
     */
    @Column(length = 512, nullable = false)
    private String clientId;
    /**
     * 客户端密码
     */
    @Column(length = 512, nullable = false)
    private String clientSecret;
    /**
     * 客户端范围
     */
    @Column()
    private String[] scope;
    /**
     * 客户端认证方式
     */
    @Column()
    private String[] authorizedGrantTypes;
    /**
     * 客户端可访问的资源ID
     */
    @Column()
    private String[] resourceIds;
    /**
     * 客户端可访问的资源ID
     */
    @Column()
    private String[] redirectUris;
    /**
     * 客户端认证方式
     */
    @Column()
    private String[] authorities;
    /**
     * token有效时间
     */
    @Column()
    private Integer accessTokenValiditySeconds = 1800;
    /**
     * token有效时间
     */
    @Column()
    private Integer refreshTokenValiditySeconds = 1800;
    /**
     * 是否自动授权
     */
    @Column()
    private Boolean autoApprove;

//    @Override
//    public boolean isSecretRequired() {
//        return true;
//    }
//
//    @Override
//    public boolean isScoped() {
//        return StringUtils.isNotEmpty(this.getScopes());
//    }
//
//    @Override
//    public Set<String> getScope() {
//        HashSet<String> result = new HashSet<>();
//        if (isScoped()) {
//            String[] arr = this.scopes.split(",");
//            for (String item : arr) {
//                result.add(item);
//            }
//        }
//
//        return result;
//    }
//
//    @Override
//    public Set<String> getRegisteredRedirectUri() {
//        HashSet<String> result = new HashSet<>();
//        if (StringUtils.isNotEmpty(this.redirectUris)) {
//            String[] arr = this.redirectUris.split(",");
//            for (String item : arr) {
//                result.add(item);
//            }
//        }
//        return result;
//    }
//
//    @Override
//    public Collection<GrantedAuthority> getAuthorities() {
//        Collection<GrantedAuthority> result = new HashSet<>();
//        if (StringUtils.isNotEmpty(this.authorities)) {
//            String[] arr = this.redirectUris.split(",");
//            for (String item : arr) {
//                result.add(new SimpleGrantedAuthority(item));
//            }
//        }
//        return result;
//    }
//
//    @Override
//    public Integer getRefreshTokenValiditySeconds() {
//        return null;
//    }
//
//    @Override
//    public boolean isAutoApprove(String s) {
//        return this.autoApprove;
//    }
//
//    @Override
//    public Map<String, Object> getAdditionalInformation() {
//        return this.additionalInformation;
//    }
}
